import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:initproject/config/ConfigApi.dart';
import 'package:initproject/config/ConfigColors.dart';
import 'package:initproject/config/ConfigContext.dart';
import 'package:initproject/config/ConfigDefaultPageStyle.dart';
import 'package:initproject/const/LgConst.dart';
import 'package:initproject/entitys/LanguageEntity.dart';
import 'package:initproject/pages/tabbar/TabbarPage.dart';
import 'package:mybaseplus/base/abstract/IFFScaffold.dart';
import 'package:mybaseplus/base/config/ColorConfig.dart';
import 'package:mybaseplus/base/config/DeaultPageConfig.dart';
import 'package:mybaseplus/base/tool/api/ApiUtils.dart';
import 'package:mybaseplus/base/tool/color/FFColor.dart';
import 'package:mybaseplus/base/tool/language/FFMultiLanguage.dart';

class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  MyAppState createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> implements FFMainAppState {

  @override
  void initState() {
    super.initState();
    config();

  }

  ///必要的配置
  config(){
    //接口环境配置
    ApiUtils.appApi = ConfigApi();

    //颜色配置
    ColorConfig().bodyColor = ConfigColors.bodyColor;
    ColorConfig().statusBackgroundColor = ConfigColors.statusBackgroundColor;
    ColorConfig().statusTextColor = ConfigColors.statusTextColor;
    ColorConfig().splashColor = ConfigColors.splashColor;

    AppLocale.delegate = AppLocaleDelegate(
      languagePath: 'assets/language/',
      onFuncCreateEntity: (Map<String, dynamic> json) {
        LanguageEntity entity = LanguageEntity.fromJson(json);
        return entity;
      },
    );


    //默认页面配置
    ConfigDefaultPageStyle.init();
    //语言代理
    FFMultiLanguage.localizationsDelegates = [
      // AppLocale.delegate!,
      DeaultPageConfig.refreshDelegate,
      GlobalMaterialLocalizations.delegate,
      GlobalCupertinoLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
    ];
    //语言回调对象
    FFMultiLanguage.state = this;
    //FFMultiLanguage.supportedLocalesList = [];

  }

  @override
  onLanguageRefresh() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // debugShowCheckedModeBanner: true,
      navigatorObservers: [routeObserver, FlutterSmartDialog.observer],
      navigatorKey: navigatorKey,
      onGenerateTitle: (context) {
        return LgConst.onTitle(context);
      },
      localizationsDelegates: FFMultiLanguage.localizationsDelegates,
      supportedLocales: FFMultiLanguage.supportedLocales(),
      locale: FFMultiLanguage.currentLocale,
      localeResolutionCallback:
          (Locale? locale, Iterable<Locale>? localeResolutionCallback) {
        Locale realLocale = FFMultiLanguage.mainSupportedLocales(locale);
        return realLocale;
      },
      theme: ThemeData(
        primarySwatch: FFColor.createMaterialColor(ConfigColors.bodyColor!),
        primaryColor: FFColor.createMaterialColor(ConfigColors.bodyColor!),
        platform: TargetPlatform.iOS,
      ),
      builder: FlutterSmartDialog.init(),
      home: ScreenUtilInit(
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (ctx, child) {
          return TabbarPage();
        },
      ),
    );
  }


}
